<template>
  <div class="login">
    <div class="mylogin" align="center">
      <h1 style="color: aliceblue">权限管理系统</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item prop="userName">
        <span slot="label">
        <span style="color: white">用户名</span>
        </span>
        <el-input style="background-color: #c1c1c1" v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item  prop="password">
         <span slot="label">
        <span style="color: white">密码</span>
        </span>
        <el-input v-model="ruleForm.password" show-password></el-input>
      </el-form-item>
      <el-row :gutter="0">
        <el-col :span="16">
          <el-form-item prop="picCode" >
             <span slot="label">
        <span style="color: white">图片验证码</span>
        </span>
            <el-input v-model="ruleForm.picCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="text-align: right">
          <img style="margin-top: 2px; width: 110px;height: 38px"
               :src="picUrl"
               @click="changePic()"
          />
        </el-col>
      </el-row>

      <el-form-item style="text-align: center;margin-right:85px;margin-top: 55px">
        <el-button  type="primary" @click="submitForm('ruleForm')">登录</el-button>
<!--        <el-button @click="resetForm('ruleForm')">重置</el-button>-->
      </el-form-item>
    </el-form>
    </div>
  </div>
</template>

<script>
import user from "../api/user";


export default {
  data() {
    return {
      picUrl: '',
      ruleForm: {
        uuid: sessionStorage.getItem('uuid'),
        userName: '',
        password: '',
        picCode: ''
      },
      rules: {
        userName: [
          {required: true, message: '用户名不为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不为空', trigger: 'blur'}
        ],
        picCode: [
          {required: true, message: '图片验证码不为空', trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.changePic()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 执行登录
          let result = await user.login(this.ruleForm);
          console.log(result)
          sessionStorage.setItem('token', result.data)
          sessionStorage.setItem('expireTime', new Date().getTime() + 24 * 60 * 60 * 1000)
          let authMenu = await user.getAuthMenu();
          console.log('----',authMenu,'--------')
          sessionStorage.setItem('userMenu', JSON.stringify(authMenu.data))

           this.$router.push('/about')

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    changePic() {
      console.log("changePic")
      this.picUrl = 'http://localhost:9000/sys/getPicCode?uuid=' + sessionStorage.getItem('uuid') +'&time=' + new Date().getTime()
      // this.picUrl = 'http://localhost/api/sys/getPicCode?uuid=' + sessionStorage.getItem('uuid') +'&time=' + new Date().getTime()
    }
  }
}
</script>
<style>
html,body{
  margin:0;
  padding:0;
}
</style>
<style scoped>
.login {
  width: 100vw;
  padding: 0;
  margin: 0;
  height: 100vh;
  font-size: 16px;
  background-position: left top;
  background-color: #242645;
  color: #fff;
  font-family: "Source Sans Pro";
  position: relative;
  background-image: url("../assets/bgcolor.jpg");
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
}

.mylogin {
  width: 350px;
  height: 380px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  padding: 50px 40px 40px 40px;
  box-shadow: -15px 15px 15px rgba(6, 17, 47, 0.7);
  opacity: 1;

  background: linear-gradient(
      230deg,
      rgba(53, 57, 74, 0) 0%,
      rgb(0, 0, 0) 100%
  );
}

.el-form-item label{
  color: #0c0c0c;
}

.submitBtn {
  background-color: transparent;
  color: #39f;
  width: 200px;
}
</style>